<?php
namespace Org\Oa;
/**
 * 团购、限时抢购
 * @author Administrator
 *
 */
class groupbuy{
	/**
	 * 获取团购、限时抢购
	 */
	public static function groupbuy_list($type,$firstrow=0,$listrow=0){
		$db = M('group_buying as pr');
		$today = date("Y-m-d H:i:s");
		$list = $db->join("left join __PRODUCTS__ as p on pr.goods_id=p.id left join __GOODS__ as g on g.id = p.goods_id")->where("pr.start_time<='{$today}' and pr.end_time >= '{$today}' and pr.type='{$type}' and pr.status='0' and pr.stor_num > '0' and g.name != ''")->field("g.name,pr.price,p.sell_price,pr.stor_num,g.name,p.spec_array,SUBSTRING_INDEX(p.photo_list,',',1) as product_img,g.img,p.goods_id,pr.end_time,p.id as product_id,pr.type")->limit($firstrow,$listrow)->order("pr.id desc")->select();
		return $list;
	}
	/**
	 * 判断该商品 在团购、限时抢购中是否过期
	 * $goods_id 产品id
	 */
	public static function groupbuy_is_out($type,$goods_id){
		$db = M('group_buying as pr');
		$today = date("Y-m-d H:i:s");
		$list = $db->where("pr.start_time<='{$today}' and pr.end_time >= '{$today}' and pr.type='{$type}' and pr.status='0' and pr.stor_num > '0' and goods_id='{$goods_id}'")->order("id desc")->find();
		return $list;
	}
	/**
	 * 减库存
	 */
	public static function groupbuy_del_num($type,$goods_id,$num){
		$db = M('group_buying as pr');
		$today = date("Y-m-d H:i:s");
		$list = $db->where("pr.type='{$type}' and pr.status='0' and pr.stor_num > '0' and goods_id='{$goods_id}'")->order("id desc")->find();
		if($list){
			$db->where("id='{$list['id']}'")->setDec('stor_num',$num);
			return true;
		}else{
			return false;
		}
	}
}